using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using WozLib.WozCommands;
using WozLib.WozObjects;
using System.Windows.Controls;

namespace WOzTestUI.WozCommands
{
  public interface IListboxWindow : IServerObject
  {
    ListBox ScreensListBox
    {
      get;
    }
	
  }

  public class WozScreenSelectCommand : WozObjectWozCommand
  {
    [XmlAttribute("PreviousSelectionServerID")]
    public long PreviousSelectionServerID
    {
      get
      {
        return _PreviousSelectionServerID;
      }
      set
      {
        _PreviousSelectionServerID = value;
      }
    } long _PreviousSelectionServerID;

    [XmlAttribute("NewSelectionServerID")]
    public long NewSelectionServerID
    {
      get
      {
        return _NewSelectionServerID;
      }
      set
      {
        _NewSelectionServerID = value;
      }
    } long _NewSelectionServerID;

    [XmlAttribute("WozCommitChangesCommand")]
    public WozCommitChangesCommand WozCommitChangesCommand
    {
      get
      {
        return _WozCommitChangesCommand;
      }
      set
      {
        _WozCommitChangesCommand = value;
      }
    } WozCommitChangesCommand _WozCommitChangesCommand;

    public WozScreenSelectCommand()
    {
    }

    public WozScreenSelectCommand(IListboxWindow iListboxWindow, long previousSelectionServerID, long newSelectionServerID)
      : base(iListboxWindow)
    {
      PreviousSelectionServerID = previousSelectionServerID;
      NewSelectionServerID = newSelectionServerID;
    }

    public WozScreenSelectCommand(IListboxWindow iListboxWindow, long previousSelectionServerID, long newSelectionServerID, WozCommitChangesCommand wozCommitChangesCommand)
      : base(iListboxWindow)
    {
      PreviousSelectionServerID = previousSelectionServerID;
      NewSelectionServerID = newSelectionServerID;
      WozCommitChangesCommand = wozCommitChangesCommand;
    }

    /// <summary>
    /// Returns the WizardWindow that is the target of this command. Returns null if none exists.
    /// </summary>
    [XmlIgnore]
    public IListboxWindow TargetListboxWindow
    {
      get
      {
        IServerObject iServerObject = TargetIServerObject;
        if (iServerObject is IListboxWindow)
        {
          return iServerObject as IListboxWindow;
        }
        else
        {
          return null;
        }
      }
      set
      {
        TargetServerID = value.ServerID;
      }
    }

    public override void Undo()
    {

      if (TargetListboxWindow != null && TargetListboxWindow.ScreensListBox != null)
      {
        if (WozCommitChangesCommand != null)
        {
          WozCommitChangesCommand.Undo();
        }

        if (TargetListboxWindow.ScreensListBox.Items.Contains(ServerObjectRegistry.GetObject(PreviousSelectionServerID)))
        {
          TargetListboxWindow.ScreensListBox.SelectedItem = ServerObjectRegistry.GetObject(PreviousSelectionServerID);
        }
      }
    }

    public override void Redo()
    {

      if (TargetListboxWindow != null && TargetListboxWindow.ScreensListBox != null)
      {
        if (TargetListboxWindow.ScreensListBox.Items.Contains(ServerObjectRegistry.GetObject(NewSelectionServerID)))
        {
          TargetListboxWindow.ScreensListBox.SelectedItem = ServerObjectRegistry.GetObject(NewSelectionServerID);
        }

        if (WozCommitChangesCommand != null)
        {
          WozCommitChangesCommand.Redo();
        }
      }
    }
  }
}
